<!-- 开发部：材料出库作业：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '10px 10px 0' }" class="mt10">
        <div slot="header" class="f16">
          <span style="font-weight: 600" class="mr55">扫描备料单条码</span>
        </div>
        <div class="flex mt10" style="align-items: center">
          <div class="f14 mr55">PS:扫描指令单标签时，请确认光标置于此栏位</div>
          <div class="f14 mr55">
            <span class="mr10">扫描条码</span>
            <el-input @keydown="(event:any) => handleKeyDown(event)" v-model="scanCode" style="width: 414px; height: 25px; font-size: 20px" />
          </div>
          <div class="flex AIC">
            <span class="mr10">扫描信息</span>
            <div :class="flagBC === '0' ? '' : flagBC === '1' ? 'susses' : 'error'" style="width: 250px; height: 25px; border: 1px solid black">
              {{ scanMessage }}
            </div>
          </div>
        </div>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="f16 flex">
          <span style="font-weight: 600">扫描单据资料</span>
          <el-button class="ml10 primaryBth" type="primary" size="default" @click="autoFill">自动填充出库数量</el-button>
          <el-button style="background-color: #ce9178" class="ml10 primaryBth" type="primary" size="default" @click="issueOrder"
            >生成出库单</el-button
          >
        </div>
        <Table :tableHeader="tableHeader" :tableData="tableData" :TableHeight="50" :outTableHeight="230" :isLoading="isLoading" />
        <div class="flex mt10" style="align-items: center">
          <span class="mr15" style="color: #5daeff; font-size: 12px">替换前材料信息</span>

          <el-form-item class="OneAwa mr24" label="材质" style="font-size: 14px">
            <el-input style="width: 414px; height: 25px" />
          </el-form-item>

          <el-form-item class="OneAwa mr24" label="颜色" style="font-size: 14px">
            <el-input style="width: 74px; height: 25px" />
          </el-form-item>

          <el-form-item class="OneAwa mr24" label="规格" style="font-size: 14px">
            <el-input style="width: 74px; height: 25px" />
          </el-form-item>

          <el-form-item class="OneAwa" label="单位" style="font-size: 14px">
            <el-input style="width: 74px; height: 25px" />
          </el-form-item>
        </div>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '0px 10px 5px' }">
        <div slot="header" class="f16">
          <span style="font-weight: 600">已出库资料</span>
        </div>
        <Table :tableHeader="tableHeader2" :tableData="tableData2" :TableHeight="50" :outTableHeight="230" :summaryData="summaryData2" summary />
        <div class="flex" style="align-items: center">
          <span style="color: #5daeff; font-size: 12px" class="mr15">替换前材料信息</span>

          <el-form-item class="OneAwa mr24" label="材质" style="font-size: 14px">
            <el-input style="width: 414px; height: 25px" />
          </el-form-item>

          <el-form-item class="OneAwa mr24" label="颜色" style="font-size: 14px">
            <el-input style="width: 74px; height: 25px" />
          </el-form-item>

          <el-form-item class="OneAwa mr24" label="规格" style="font-size: 14px">
            <el-input style="width: 74px; height: 25px" />
          </el-form-item>

          <el-form-item class="OneAwa mr24" label="单位" style="font-size: 14px">
            <el-input style="width: 74px; height: 25px" />
          </el-form-item>
        </div>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '0px 5px 5px' }">
        <PageModule :PageData="{ total: total2, buttonS: true }" @pageSizeChange="handleSizeChange2" @pageChange="handleCurrentChange2" />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'MaterialDelivery'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onBeforeUnmount, onMounted, watch } from 'vue'
const scanCode = ref('')
const flagBC = ref('0')
const scanMessage = ref('')

const handleKeyDown = async (e: any) => {
  if (e.key === 'Enter' || e.keyCode === 13) {
    const res = (await LoginWork.get(`/stock/out/scanSlipByLotNo?lotNo=${scanCode.value}`)) as any
    if (res.code === 200) {
      JMessage.success('扫描成功')
      getTableList()
      const tempCode = JSON.parse(JSON.stringify(scanCode.value))
      scanCode.value = ''
      flagBC.value = '1'
      scanMessage.value = tempCode
    } else {
      JMessage.error(res.message)
      flagBC.value = '2'
      scanMessage.value = res.message
    }
  }
}

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入出库单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入备料单号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '出库日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材质',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入颜色',
    inputValue: ''
  }
])

const isLoading = ref(false)
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const tableHeader = ref([
  {
    change: true,
    label: '指令号',
    prop: 'lotNo',
    width: '150px'
  },
  {
    change: true,
    label: '订购单号',
    prop: 'purNo',
    width: '150px'
  },
  {
    change: true,
    label: '备料员',
    prop: 'userId',
    width: '114px'
  },
  {
    change: true,
    label: '备料日期',
    prop: 'sysDate',
    width: '70px'
  },
  {
    change: true,
    label: '材质',
    prop: 'mtlName',
    width: '150px'
  },
  {
    change: true,
    label: '颜色',
    prop: 'mtlColor',
    width: '76px'
  },
  {
    change: true,
    label: '规格',
    prop: 'mtlSpec',
    width: '76px'
  },
  {
    change: true,
    label: '单位',
    prop: 'mtlUnit',
    width: '76px'
  },
  {
    change: true,
    label: '备料量',
    prop: 'qty',
    width: '76px'
  },
  {
    change: true,
    label: '已出数量',
    prop: 'isOutQty',
    width: '76px'
  },
  {
    change: true,
    label: '未出数量',
    prop: 'unQty',
    width: '76px'
  },
  {
    change: true,
    label: '在制库存',
    prop: 'stkQty',
    width: '76px'
  },
  {
    change: true,
    label: '在制出库库存位置',
    prop: 'areaNo'
  },
  {
    change: true,
    label: '库存位置库存数量',
    prop: 'warehouseQty'
  },
  {
    type: 'dateSingle',
    label: '出库日期',
    prop: 'outDate',
    width: '120px'
  },
  {
    disable: true,
    label: '出库数量',
    prop: 'outQty',
    width: '76px'
  },
  {
    change: true,
    label: '备注',
    prop: 'remark',
    width: '244px'
  }
])
const getTableList = debounce(async (flag: any = 1) => {
  isLoading.value = true
  let tableDataList = await service.get<any, any>(`/stock/out/getScanSlipByLotNo`)
  isLoading.value = false
  tableData.value = tableDataList.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    if (!item.warehouseQty || !item.areaNo) item.disable = true
    return item
  })
}, 500)

// 表二模块
const tableHeader2 = ref([
  {
    label: '指令号',
    prop: 'lotNo',
    width: '189px'
  },
  {
    label: '出库单号',
    prop: 'slipNo',
    width: '189px'
  },
  {
    label: '出库日期',
    prop: 'slipDate',
    width: '189px'
  },
  {
    label: '材质',
    prop: 'mtlName'
  },
  {
    label: '颜色',
    prop: 'mtlColor',
    width: '95px'
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    width: '110px'
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    width: '110px'
  },
  {
    label: '出库数量',
    prop: 'outQty',
    width: '110px'
  },
  {
    label: '库存位置',
    prop: 'areaNo',
    width: '110px'
  },
  {
    label: '备注',
    prop: 'remark',
    width: '110px'
  }
])
// 表二按钮
const tableData2 = ref([])
const isLoading2 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 8,
    num: 0
  }
])
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.get<any, any>('/stock/out/outMtlList', {
    params: {
      pageNum: currentPage2.value,
      pageSize: pageSize2.value
    }
  })
  isLoading2.value = false
  tableData2.value = tableDataList.list
  total2.value = tableDataList.total
  summaryData2.value[0].num = tableDataList.outTotal
}, 500)
onMounted(() => {
  getTableList2()
})

onBeforeUnmount(async () => {
  await service.post('/stock/other/deleteByTemp')
})

const autoFill = async () => {
  tableData.value.forEach((item: any) => {
    if (item.warehouseQty) item.outQty = item.unQty
  })
  if (tableData.value.some((item: any) => !item.warehouseQty)) JMessage.warning('请检查库存位置库存数量')
}
const issueOrder = async () => {
  if (tableData.value.some((item: any) => Number(item.warehouseQty) > Number(item.stkQty))) {
    JMessage.warning('出库数量不能大于在制库存数量')
    return
  }
  const res = await LoginWork.post<any, any>('/stock/out/createOutSlipNo', tableData.value)
  if (res.code === 200) {
    JMessage.success('生成出库单单据成功')
    getTableList()
    getTableList2()
  } else {
    JMessage.warning(res.message)
  }
}
</script>

<style lang="less" scoped>
.OneAwa {
  margin-bottom: 0px;
}
.susses {
  background-color: rgb(71, 232, 71);
  text-align: center;
  line-height: 25px;
  font-size: 20px;
}
.error {
  background-color: #ff0000;
  font-size: 20px;
  text-align: center;
}
</style>
